package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.PublishReqBean;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.GetTypeListBean;
import com.cljz.makepassenger.model.net.bean.InviteContentBean;
import com.cljz.makepassenger.model.net.bean.InviteDetailBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.EditorInvitedActivity;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/7.
 */

public class EditorInvitedP extends BaseP<EditorInvitedActivity> {
    public void getInviteContent() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("self_config", "v2_1");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getInviteContentData(requestBody)
                .compose(XApi.<InviteContentBean>getApiTransformer())         //异常处理
                .compose(XApi.<InviteContentBean>getScheduler())              //线程切换
                .compose(getV().<InviteContentBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<InviteContentBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(InviteContentBean inviteContentBean) {
                        if ("0".equals(inviteContentBean.getError_code())) {
                            getV().inviteContentSucceed(inviteContentBean.getData());
                        } else if ("1000".equals(inviteContentBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().inviteContentFail(inviteContentBean.getMsg());
                        }
                    }
                });
    }

    public void getInviteUpdate(String cityId, String flagId, InviteDetailBean.DataBean dataBean) {
//        try {
//            HashMap<String, String> paramsMap = new HashMap<>();
//            paramsMap.put("token", getToken());
//            paramsMap.put("project_id", dataBean.getId());
//            paramsMap.put("content", dataBean.getContent());
//            paramsMap.put("project_time", GetSystemTimeUtil.dateToStamp(dataBean.getTime()).toString());
//            paramsMap.put("project_address", dataBean.getShop_name() + "===" + dataBean.getProject_address());
//            paramsMap.put("invite_id", flagId);
//            paramsMap.put("city_id", cityId);
//            paramsMap.put("longitude", dataBean.getLatitude());
//            paramsMap.put("latitude", dataBean.getLongitude());
//            String body = Base64Utils.enCode(gson.toJson(paramsMap));
//            final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
//            Api.getResponseInfoAPI().getInviteUpdateData(requestBody)
//                    .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
//                    .compose(XApi.<ResponseBean>getScheduler())              //线程切换
//                    .compose(getV().<ResponseBean>bindToLifecycle())
//                    .subscribe(new ApiSubcriber<ResponseBean>() {
//                        @Override
//                        protected void onFail(NetError error) {
//                        }
//
//                        @Override
//                        public void onNext(ResponseBean responseBean) {
//                            if ("0".equals(responseBean.getError_code())) {
//                                getV().inviteUpdateSucceed(responseBean.getMsg());
//                            } else if ("1000".equals(responseBean.getError_code())) {
//                                getV().loginOvertime(1);
//                            } else {
//                                getV().inviteUpdateFail(responseBean.getMsg());
//                            }
//                        }
//                    });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public void getTypeList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("type", "activity");
        paramsMap.put("is_publish", "1");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getTypeListData(requestBody)
                .compose(XApi.<GetTypeListBean>getApiTransformer())
                .compose(XApi.<GetTypeListBean>getScheduler())
                .compose(getV().<GetTypeListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetTypeListBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(GetTypeListBean getTypeListBean) {
                        if ("0".equals(getTypeListBean.getError_code())) {
                            getV().getTypeListSucceed(getTypeListBean.getData());
                        } else if ("1000".equals(getTypeListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTypeListFail(getTypeListBean.getMsg());
                        }
                    }
                });
    }

    public void inviteUpdate(PublishReqBean publishReqBean, String invite_id) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("invite_id", invite_id);
        paramsMap.put("invite_time", GetSystemTimeUtil.switchTimestamp(publishReqBean.getInvite_time()));
        paramsMap.put("landmark", publishReqBean.getLandmark());
        paramsMap.put("address", publishReqBean.getAddress());
        paramsMap.put("title", publishReqBean.getTitle());
        paramsMap.put("region_id3", publishReqBean.getRegion_id3());
        paramsMap.put("longitude", publishReqBean.getLongitude());
        paramsMap.put("latitude", publishReqBean.getLatitude());
        paramsMap.put("invitation_id", publishReqBean.getInvitation_id());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getInviteUpdateData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().inviteUpdateSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().inviteUpdateFail(responseBean.getMsg());
                        }
                    }
                });
    }
}
